<template>
	<view>
		<view class="content">
			<view class="title">
				联系人信息
			</view>
			<view class="row">
				<view class="nominal">
					收件人
				</view>
				<view class="input">
					<input placeholder="请输入收件人姓名" type="text" v-model="name" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="number" v-model="tel" />
				</view>
			</view>
			<view class="title">
				服务地址信息
			</view>
			<view class="row">
				<view class="nominal">
					所在地区
				</view>
				<view class="input">
					<picker mode="multiSelector" :range="citylist" @change="changecity" @columnchange="columnchangecity"
						:value="value">
						<view>{{region[0]}}-{{region[1]}}-{{region[2]}}</view>
					</picker>
				</view>

			</view>
			<view class="row">
				<view class="nominal">
					详细地址
				</view>
				<view class="input">
					<textarea v-model="detailed" auto-height="true" placeholder="输入详细地址"></textarea>
				</view>
			</view>
			<!-- <view class="row" style="margin-top: 15px;">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch color="#2BC360" :checked="isDefault" @change="isDefaultChange" />
				</view>
			</view> -->
		</view>

		<view class="save" @tap="save">
			<view class="btn">
				保存地址
			</view>
		</view>
		<!-- <mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue"
			@onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker> -->
	</view>
</template>

<script>
	import provinceData from '../city-data/province.js'
	import cityData from '../city-data/city.js';
	import areaData from '../city-data/area.js';
	import {
		AddressAdd
	} from "@/api/api.js"
	// import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	export default {
		// components: {
		// 	mpvueCityPicker
		// },
		data() {
			return {
				editType: 'edit',
				id: '',
				name: '',
				tel: '',
				detailed: '',
				isDefault: false,
				cityPickerValue: [2, 0, 0],
				themeColor: '#2BC360',
				citylist: [],
				value: [0, 0, 0],
				region: ['省', '市', '区']
			};
		},
		watch: {
			value() {
				this.init();
			}
		},
		methods: {

			init() {
				var pro = provinceData.map(item => {
					return item.label
				})

				var city = cityData[this.value[0]].map(item => {
					return item.label
				})
				var area = areaData[this.value[0]][this.value[1]].map(item => {
					return item.label
				})
				this.$set(this.citylist, 0, pro);
				this.$set(this.citylist, 1, city);
				this.$set(this.citylist, 2, area);

			},
			changecity(e) {

				this.region = [this.citylist[0][e.detail.value[0]],
					this.citylist[1][e.detail.value[1]],
					this.citylist[2][e.detail.value[2]]
				]
			},
			columnchangecity(e) {
				let changePickerValue = e.mp.detail;
				if (changePickerValue.column == 0) {
					this.value[1] = 0
					this.value[2] = 0
				}
				this.value[changePickerValue.column] = changePickerValue.value

				this.init();
			},
			AddressAdd() {
				var isDefaultid
				if (this.isDefault) {
					isDefaultid = 1 //默认地址
				} else {
					isDefaultid = 0
				}
				if (this.region[0] == '省') {
					uni.showToast({
						title: '请选择所在地区',
						icon: 'none'
					})
					return
				}
				AddressAdd({
					id: this.id,
					name: this.name,
					phone: this.tel,
					address: this.detailed,
					region: this.region.join('-')
				}).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				})
			},
			isDefaultChange(e) {
				this.isDefault = e.detail.value;
				if (this.isDefault) {
					AddressDefault({
						address_id: this.id
					}).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					})
				}
			},
			del() {
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个收货地址',
					success: (res) => {
						AddressDelete({
							address_id: this.id
						}).then(res => {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
							setTimeout(res => {
								uni.navigateBack()
							}, 300)
						})
					}
				});

			},
			save() {

				let data = {
					"name": this.name,
					"head": this.name.substr(0, 1),
					"tel": this.tel,
					"address": {
						"region": this.region,
						"detailed": this.detailed
					},
					"isDefault": this.isDefault
				}

				if (this.editType == 'edit') {
					data.id = this.id
				}
				if (!data.name) {
					uni.showToast({
						title: '请输入收件人姓名',
						icon: 'none'
					});
					return;
				}
				if (!data.tel) {
					uni.showToast({
						title: '请输入收件人电话号码',
						icon: 'none'
					});
					return;
				}
				if (!data.address.detailed) {
					uni.showToast({
						title: '请输入收件人详细地址',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.AddressAdd()
				//实际应用中请提交ajax,模板定时器模拟提交效果


			}
		},
		onLoad(e) {
			this.init()
			//获取传递过来的参数
			this.editType = e.type;
			if (e.type == 'edit') {
				uni.getStorage({
					key: 'address',
					success: (e) => {
						this.id = e.data.id;
						this.name = e.data.name;
						this.tel = e.data.phone;
						this.detailed = e.data.address;
						if (e.data.is_default == 1) {
							this.isDefault = true
						} else {
							this.isDefault = false
						}
						// this.cityPickerValue = e.data.address.region.value;
						this.region = e.data.region.split('-');
					}
				})
			}

		},
	};
</script>
<style lang="scss">
	page {
		background: #f3f3f3;
	}

	.save {
		view {
			display: flex;
		}

		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
		max-width: 500px;

		.btn {
			width: 90%;
			height: 80upx;
			border-radius: 20upx;
			background-color: #2BC360;
			color: #fff;
			justify-content: center;
			align-items: center;

			.icon {
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}

			font-size: 30upx;
		}
	}

	.content {
		display: flex;
		flex-wrap: wrap;

		view {
			display: flex;
		}

		.title {
			padding: 10px;
			font-weight: bold;
			margin-top: 10px;
			background: transparent;
			width: 100%;
			font-size: 14px;
		}

		.row {
			width: 100%;
			padding: 0 3%;
			border-top: solid 1upx #eee;
			background: #fff;

			.nominal {
				width: 30%;
				height: 100upx;
				font-size: 28upx;
				align-items: center;
			}

			.input {
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 28upx;

				&.switch {
					justify-content: flex-end;

				}

				switch {
					transform: scale(.7);
				}

				input {
					font-size: 28upx;
				}

				textarea {
					font-size: 28upx;
					margin: 20upx 0;
					min-height: 120upx;
				}
			}

			.del {
				width: 96%;

				height: 80upx;
				border-radius: 20upx;
				justify-content: center;
				align-items: center;
				font-size: 28upx;
				color: #fff;
				background-color: rgba(255, 0, 0, 0.8);
				border-bottom: solid 1upx #eee;
				margin: 10px auto;
			}
		}
	}
</style>